import { getData, getSearch, getDetail, getbusiness, getmarketlist, gethealth } from '@/api/marketInfoMge'
import { Message } from 'element-ui';
export default {
  namespaced: true, // 封闭
  state: {
    searchList: [],
    arealist: [],
    area: [],
    stallScale: [],
    organizer: [],
    starLevel: [],
    Typelist: [],
    checkResult: []
  },
  mutations: {
    setSearchlist(state, data) {
      state.searchList = data
      // console.log(state.infoList)
      let dataarr = [];
      for (let i = 0; i < data.areaCodeList.length; i++) {
        dataarr.push({
          value: data.areaCodeList[i].id,
          lable: data.areaCodeList[i].name
        });
      }
      sessionStorage.setItem('arealist',JSON.stringify(dataarr))
      // window.localStorage.setItem('arealist',JSON.stringify(dataarr));
      // state.arealist = JSON.parse(sessionStorage.getItem(dataarr));
      // sessionStorage.setItem()

      // console.log(JSON.parse(window.localStorage.getItem('arealist')))
      // console.log(JSON.parse(sessionStorage.getItem('arealist')))

      let dataarr2 = [];
      for (let i = 0; i < data.area.length; i++) {
        dataarr2.push({
          value: data.area[i].id,
          lable: data.area[i].name
        });
      }
      sessionStorage.setItem('area',JSON.stringify(dataarr2))
      // state.area = JSON.parse(sessionStorage.getItem(dataarr2));
      // state.area = dataarr2;

      let dataarr3 = [];
      for (let i = 0; i < data.stallScale.length; i++) {
        dataarr3.push({
          value: data.stallScale[i].id,
          lable: data.stallScale[i].name
        });
      }
      sessionStorage.setItem('stallScale',JSON.stringify(dataarr3))
      // state.stallScale = dataarr3;

      let dataarr4 = [];
      for (let i = 0; i < data.organizer.length; i++) {
        dataarr4.push({
          value: data.organizer[i].id,
          lable: data.organizer[i].name
        });
      }
      // state.organizer = dataarr4;
      sessionStorage.setItem('organizer',JSON.stringify(dataarr4))

      let dataarr5 = [];
      for (let i = 0; i < data.starLevel.length; i++) {
        dataarr5.push({
          value: data.starLevel[i].id,
          lable: data.starLevel[i].name
        });
      }
      // state.starLevel = dataarr5;
      sessionStorage.setItem('starLevel',JSON.stringify(dataarr5))

      let dataarr6 = [];
      for (let i = 0; i < data.backendBusinessType.length; i++) {
        dataarr6.push({
          value: data.backendBusinessType[i].id,
          lable: data.backendBusinessType[i].name
        });
      }
      // state.Typelist = dataarr6;
      dataarr6.unshift({
        lable: "所有",
        value: "-1"
      });
      sessionStorage.setItem('Typelist',JSON.stringify(dataarr6))
      // console.log(state.Typelist);

      let dataarr7 = [];
      for (let i = 0; i < data.checkResult.length; i++) {
        dataarr7.push({
          value: data.checkResult[i].id,
          lable: data.checkResult[i].name
        });
      }
      // state.checkResult = dataarr7;
      dataarr7.splice(1,0,{
        lable: "合格",
        value: "-1"
      });

      sessionStorage.setItem('checkResult',JSON.stringify(dataarr7))
    }
  },
  getters: {},
  actions: {
    // 
    async getData(context, payload) {
      try {
        const { data } = await getData(payload)
        // console.log(data)
        return data
      } catch (err) {
        console.log(err)
      }
    },
// async getData(context, payload) {
//       try {
//         const { data } = await getData(payload)
//         .then(res=>{
//           console.log(res)
//           if(res.code==200){
//             console.log(res.data)
//             context.commit('setSearchlist', res)
//           }
//           else{
//             context.commit('setSearchlist', res)
//           }
//         })
//         // console.log("data",data)
//         // return data
//       } catch (err) {
//         console.log(err)
//       }
//     },

    async getSearch(context, payload) {
      try {
        const { data } = await getSearch(payload)
          .then(res => {
            // console.log(res)
            if (res.code == 200) {
              // console.log(res.data)
              context.commit('setSearchlist', res.data)
            }
            else {
              context.commit('setSearchlist', res.data)
            }
          })
      } catch (err) {
        // console.log(err)
      }
    },

    // async getSearch(context, payload) {
    //     try {
    //       const { data } = await getSearch(payload)
    //     //   console.log(data)
    //       return data
    //     } catch (err) {
    //       console.log(err)
    //     }
    //   },


    async getDetail(context, payload) {
      try {
        const { data } = await getDetail(payload)
          console.log(data)
        return data
      } catch (err) {
        console.log(err)
      }
    },
    async getbusiness(context, payload) {
      try {
        const { data } = await getbusiness(payload)
        //console.log(data)
        return data
      } catch (err) {
        console.log(err)
      }
    },
  

    async getmarketlist(context, payload) {
      try {
        const { data } = await getmarketlist(payload)
        //console.log(data)
        return data
      } catch (err) {
        console.log(err)
      }
    },

   
    async gethealth(context, payload) {
      try {
        const { data } = await gethealth(payload)
        //console.log(data)
        return data
      } catch (err) {
        console.log(err)

      }
    }
  }
}